<template >
    <div v-if="value">
        <el-dialog
        class="writeOffInvoiceDialogClass"
            title="修改冲销金额"
            :visible.sync="dialogShow"
            :before-close="handleClose"
            width="1000px"
            :append-to-body="true"
        >
            <el-row>
                <el-col :span="8">
                    <div class="writeOffInvoiceDialogClassTitle">可冲销数量：</div><el-input  v-model="writeRow.kcxsl" size="small" :disabled="true" ></el-input>
                </el-col>
                <el-col :span="8">
                    <div class="writeOffInvoiceDialogClassTitle">可冲销金额：</div><el-input v-model="writeRow.kcxje" size="small"  :disabled="true" ></el-input>
                </el-col>
                <el-col :span="8">
                    <div class="writeOffInvoiceDialogClassTitle">
                        可冲销税额：
                    </div>
                    <el-input v-model="writeRow.kcxse" size="small" :disabled="true"></el-input>
                </el-col>
            </el-row>
            <el-row style="margin-top:10px;">
                <el-col :span="8">
                    <div class="writeOffInvoiceDialogClassTitle">冲销数量：</div>
                    -<el-input v-model="writeRow.cxsl" :disabled="cxslDisable"  @blur="slChangeBlur" @change="slChange" size="small"   ></el-input>
                </el-col>
                <el-col :span="8">
                    <div class="writeOffInvoiceDialogClassTitle">冲销金额：</div>

                    -<el-input v-model="writeRow.cxje" :disabled="cxjeDisable"  @change="jeChange"  @blur="jeChangeBlur" size="small" ></el-input>
                </el-col>
                <el-col :span="8">
                    <div class="writeOffInvoiceDialogClassTitle">
                        冲销税额：
                    </div>
                    -<el-input v-model="writeRow.cxse" :disabled="true"  size="small"  ></el-input>
                </el-col>
            </el-row>

            <span slot="footer" class="dialog-footer">
                <el-button size="small"  @click="handleClose">
                    取消
                </el-button>
                <el-button size="small" type="primary" @click="OpenInvoice">
                    确认
                </el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>

import { valitedLimitNumber } from '@/utils/validate.js';

export default {
    name:"writeOffInvoice",
    props: {
        value: {
            type: Boolean,
            default: false,
        },
        dbRow: {
            type: Object,
            default: ()=>{},
        },
    },
    data() {
        return {
            writeRow:null,
            dialogShow:false,
        }
    },
    computed:{
        cxslDisable(){

            return (this.writeRow.chyydm == 1) // 冲红原因是1是开票有误，则不可修改
           ||  (this.writeRow.chyydm == 2 && !this.writeRow.kcxsl ) // 冲红原因是2是销货退回，没有数量只有金额
        || (this.writeRow.chyydm == 4) // 销售折让不允许操作数量
        || ((this.writeRow.chyydm == 3) &&  (this.writeRow.kcxsl<= 0))// 销售中止 ,并且没有数量
        },
        cxjeDisable(){
            return  (this.writeRow.chyydm == 1) // 冲红原因是1是开票有误，则不可修改 
            || ((this.writeRow.chyydm == 2) && (this.writeRow.kcxsl> 0))  // 冲销数量有值,就禁止操作金额和税额
        },
    },
    watch:{
        dbRow(){
            let je = this.dbRow.cxje&& this.bigNumAdd(this.dbRow.cxje ,'0') 
            let se = this.dbRow.cxse && this.bigNumAdd(this.dbRow.cxse ,'0') 
            let sl = this.dbRow.cxsl&& this.bigNumAdd(this.dbRow.cxsl ,'0') 
            let cxje =  je&&je.isNegative() ? je.abs().toString() : je.toString() || ''
            let cxse = se&& se.isNegative() ? se.abs().toString() : se.toString() || ''
            let cxsl = ''
            if(sl){
                cxsl =  sl.isNegative() ? sl.abs().toString() : sl.toString() || ''
            }
              
             this.writeRow = {
                ...this.dbRow,
                cxje,
                cxse,
                cxsl
             };

            this.dialogShow = true;

        }
    },
    methods: {
        slChange(){
            if(!this.writeRow.cxsl)return;
            this.writeRow.cxsl = this.writeRow.cxsl.replace(/[^0-9.]/g, '')
        },
        slChangeBlur(){
            if(!this.writeRow.cxsl)return;
            if(this.bigNumcomparedTo(this.writeRow.cxsl,this.writeRow.kcxsl) ){
                this.writeRow.cxsl = this.writeRow.kcxsl
            }
            this.writeRow.cxje = this.bigNumMultip(this.writeRow.cxsl , this.writeRow.dj,2)
            this.writeRow.cxse = this.bigNumMultip(this.writeRow.cxje , this.writeRow.slv)

            

            this.writeRow.cxsl = valitedLimitNumber(this.bigNumFixed(this.writeRow.cxsl,6),{
                        leftLength: 10,
                        rightLength:6,
                      })
        },
        jeChange(){
            if(!this.writeRow.cxje)return;
            this.writeRow.cxje = this.writeRow.cxje.replace(/[^0-9.]/g, '')
        },
        jeChangeBlur(){
            if(!this.writeRow.cxje)return;
            if(this.bigNumcomparedTo(this.writeRow.cxje,this.writeRow.kcxje))  {
                this.writeRow.cxje = this.writeRow.kcxje;
            }
            this.writeRow.cxse = this.bigNumMultip(this.writeRow.cxje , this.writeRow.slv)
           
            if(this.writeRow.cxsl){
                let res = valitedLimitNumber( this.bigNumDivision(this.writeRow.cxje,this.writeRow.dj).toFixed(6) ,{
                             leftLength: 10,
                             rightLength:6,
                })
                this.writeRow.cxsl = res
            }
            this.writeRow.cxje = valitedLimitNumber( this.bigNumMultip(this.writeRow.cxje,1,2),{
                             leftLength: 16,
                             rightLength: 2,
                })
        },
        handleClose() {
            this.$emit('input', false)
        },
        OpenInvoice(){
            console.log(this.writeRow)
            if(this.cxslDisable && this.writeRow.cxje == 0 ){
                return this.$message.warning('冲销金额不得为0')
            }

            if(this.cxjeDisable && this.writeRow.cxsl == 0){
                return this.$message.warning('冲销数量不得为0')
            }
            if((!this.cxjeDisable && !this.cxslDisable)){
                if(this.writeRow.cxsl == 0){
                    return this.$message.warning('冲销数量不得为0')
                }
                if(this.writeRow.cxje == 0){
                    return this.$message.warning('冲销金额不得为0')
                }
            }

            let cxje = this.bigNumLessen('0',this.writeRow.cxje || '').toString()
            let cxse = ((this.writeRow.cxse) && this.bigNumLessen('0',this.writeRow.cxse || '').toString()) || ''
            let cxsl = ((this.writeRow.cxsl) && this.bigNumLessen('0',this.writeRow.cxsl || '').toString()) || ''


            this.$emit('writeCb', {
                ...this.writeRow,
                cxje,
                cxse,
                cxsl
            })
            this.handleClose()
        },
    },
}
</script>

<style lang="scss" scoped>
.writeOffInvoiceDialogClass {
    
    .writeOffInvoiceDialogClassTitle{
        display: inline-block;
        width: 120px;
        line-height: 32px;
        text-align: right;
    }
    ::v-deep .el-dialog{
        margin-top: 15% !important;
    }
    ::v-deep .el-input{
        width: calc(100% - 130px);
        width: -ms-calc(100% - 130px); 
    }
    ::v-deep .el-input-number{
        .el-input{
            width: 100%;
        }
    }

    ::v-deep .el-dialog__body{
        padding: 10px;
    }

}


</style>